package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.model.SelectItem;

import com.corpcredit.Constants;
import com.corpcredit.model.Glry;
import com.corpcredit.model.GlryCwfzr;
import com.corpcredit.model.Hyfl;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class GlryCwfzrForm extends BasePage implements Serializable {
    private GenericManager<GlryCwfzr, Long> glryCwfzrManager;
    private GenericManager<Glry, Long> glryManager;
    private GlryCwfzr glryCwfzr = new GlryCwfzr();
    private Long id;
    private List<SelectItem> allGlrys =new ArrayList<SelectItem>();

    private Long glryId;
    
    public Long getGlryId()
    {
    	 if( id != null && id != 0 )
         {
             this.glryId = glryCwfzr.getGlry().getId();
             log.debug( "======in edit page glryId========"+ glryId );
             return glryId;
         }
         else
         {
             return null;
         }
    }
    public void setGlryId(Long glryId)
    {
    	this.glryId=glryId;
    }
    public void setGlryCwfzrManager(GenericManager<GlryCwfzr, Long> glryCwfzrManager) {
        this.glryCwfzrManager = glryCwfzrManager;
    }
    public void setGlryManager(GenericManager<Glry, Long> glryManager) {
        this.glryManager = glryManager;
    }
    public GlryCwfzr getGlryCwfzr() {
        return glryCwfzr;
    }

    public void setGlryCwfzr(GlryCwfzr glryCwfzr) {
        this.glryCwfzr = glryCwfzr;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<SelectItem> getAllGlrys()
    {
         List<SelectItem> glryList = new ArrayList<SelectItem>();
         glryList.add(new SelectItem("","请选择管理人员"));
         List<Glry> glrys = glryManager.getAll();
         
         for (Glry glry : glrys) 
         {
        	 glryList.add(new SelectItem(glry.getId(), glry.getUsername()));
         }
         return glryList;
    }
    public String delete() {
        glryCwfzrManager.remove(glryCwfzr.getId());
        addMessage("glryCwfzr.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
        	allGlrys=getAllGlrys();
            glryCwfzr = glryCwfzrManager.get(id);
        } else {
        	allGlrys=getAllGlrys();
            glryCwfzr = new GlryCwfzr();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (glryCwfzr.getId() == null || glryCwfzr.getId() == 0);
        
        if(glryId!=null||glryId!=0)
        {
        	glryCwfzr.setGlry(glryManager.get(glryId));
        }
        glryCwfzr.setUsername(getRequest().getRemoteUser());
        glryCwfzr.setOptdate(new Date());
        glryCwfzr.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        

        
        glryCwfzrManager.save(glryCwfzr);

        String key = (isNew) ? "glryCwfzr.added" : "glryCwfzr.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	  this.id=this.glryCwfzr.getId();
              return edit();
        }
    }
} 